package org.vaillant.todo

import org.vaillant.todo.common.helpers.ListHelper;

class TodoController {

	def taskService

	def final TASK_LIST_SIZE = 10

	def index = {
		def user = session.user

		if (params.filterTask){
			session.filterTask = params.filterTask
		}


		if (params.orderTask){
			session.orderTask = params.orderTask
		}


		List statusHistoryList = taskService.getCurrentsStatusHistoryByUser(user, session.filterTask)
		def tasksCount = statusHistoryList.size()
		
		statusHistoryList = taskService.sortStatusHistory(statusHistoryList, session.orderTask)
		
		int offset = 0
		if (params.offset){
			offset = new Integer(params.offset)
		}
		
		statusHistoryList = ListHelper.truncate(statusHistoryList, offset, TASK_LIST_SIZE )

		
		def sortMenuItemSelected = params.sortMenuItemSelected

		if (!sortMenuItemSelected){
			sortMenuItemSelected = session.sortMenuItemSelected?:'0'
		}else{
			session.sortMenuItemSelected = sortMenuItemSelected
		}

		def filterMenuItemSelected = params.filterMenuItemSelected

		if (!filterMenuItemSelected){
			filterMenuItemSelected = session.filterMenuItemSelected?:'0'
		}else{
			session.filterMenuItemSelected = filterMenuItemSelected
		}

		[tasksCount:tasksCount, statusHistoryList : statusHistoryList, sortMenuItemSelected : sortMenuItemSelected,filterMenuItemSelected : filterMenuItemSelected]
	}

	def sortByDate = {

		def user = session.user
	}

	def add = {

		def description = params.newTask

		def user = session.user

		if (description){
			taskService.createNewTask(user, description)
		}else{
			flash.message = "Task description cannot be empty!"
		}

		redirect(action:'index')
	}

	def finishedTask = {

		def taskId = params.taskId

		if (taskId){
			taskService.finishTask(taskId)
		}

		redirect(action:'index')
	}

	def addAjax = {

		def description = params.newTask

		def user = session.user

		if (!user.isAttached()) {
			user.attach()
		}

		if (description){
			taskService.createNewTask(user, description)
		}

		def tasksCount = Task.findAllByOwner(user).size()

		def statusHistoryList = taskService.getCurrentsStatusHistoryByUser(user, session.filterTask)

		int offset = 0
		if (params.offset){
			offset = new Integer(params.offset)
		}
		
		statusHistoryList = ListHelper.truncate(taskService.sortStatusHistory(statusHistoryList, session.orderTask), offset,TASK_LIST_SIZE )

		render(template:"/todo/tasksList",model: [tasksCount:tasksCount, statusHistoryList:statusHistoryList])
	}

	def updateList = {

		def user = session.user

		println params.offset

		def statusHistoryList = taskService.getCurrentsStatusHistoryByUser(user, session.filterTask)
		def tasksCount = statusHistoryList.size()
		int offset = 0
		if (params.offset){
			offset = new Integer(params.offset)
		}
		statusHistoryList = ListHelper.truncate(taskService.sortStatusHistory(statusHistoryList, session.orderTask), offset,TASK_LIST_SIZE )
		render(template:"/todo/tasksList",model: [tasksCount:tasksCount, statusHistoryList:statusHistoryList])
	}
}
